package com.defysoft.waukids.diary;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.defysoft.waukids.BaseActivity;
import com.defysoft.waukids.Custom_Data_Type.DiaryDetailLoad;
import com.defysoft.waukids.Data_Sharedpreferences;
import com.defysoft.waukids.Data_Type.WK_DIARY_PHOTO;
import com.defysoft.waukids.Data_Type.WK_DIARY_REPLY;
import com.defysoft.waukids.JsonParser;
import com.defysoft.waukids.PublicConstant;
import com.defysoft.waukids.PublicSharedpreferences;
import com.defysoft.waukids.R;
import com.defysoft.waukids.Sqlite_Connect;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class DiaryDetailActivity extends BaseActivity implements OnClickListener{
	TextView textView[] = new TextView[4];
	ImageView imageView[] = new ImageView[10];
	LinearLayout linearLayout[] = new LinearLayout[10];
	Button button[] = new Button[2];
	Intent intent;
	ArrayList<WK_DIARY_PHOTO> photoList; 
	ArrayList<WK_DIARY_REPLY> replyList; 
	Toolbar toolbar;
	Spinner spinner;
	String[] celebrities = {"수정", "삭제"};
	// 이미지
	DisplayImageOptions options;
	ImageLoader imageLoader;
	Sqlite_Connect sc;
	DiaryDetailLoad diaryDetailLoad;
	String diaryCode = "";
	int photo_count;
	PublicSharedpreferences psf;
	Data_Sharedpreferences.SF_Member member;
	Data_Sharedpreferences.SF_Student student;
	
	//덧글용 추가.
	EditText editText;
	LinearLayout replyLinearLayout;
	ListView replyListView;
	DiaryDetailReplyAdapter replyAdapter;
	ScrollView scrollView;
	InputMethodManager ipm;
	
	//1분전 세팅.
	String now_date;
	long now = System.currentTimeMillis();
	Date date = new Date(now);
	SimpleDateFormat CurYearFormat ;
	SimpleDateFormat CurMonthFormat;
	SimpleDateFormat CurDayFormat ;
	SimpleDateFormat CurHourFormat ;
	SimpleDateFormat CurMinuteFormat;
	SimpleDateFormat CurSecondFormat;
	
	private final int FIRST_THREAD = 0;
	private final int REPLY_THREAD = 1;
	private final int DELETE_THREAD = 2;
	private final int SUCCESS_DELETE = 5;
	private final int FAIR_DELETE = 6;
	private final int SUCCESS_REPLY = 7;
	private final int FAIR_REPLY = 8;
	private final int SUCCESS_FIRST = 9;
	private final int FAIR_FIRST = 10;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case START_DIARY_DETAIL: // 9110
			if (resultCode == REFRESH_CODE) {	//5555
				setResult(REFRESH_CODE);
				setStartData();
				startNetwork(FIRST_THREAD);
			}
			break;
		}		
	}
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		if (getIntent().getStringExtra("PK_Dia_Code") == null
				|| getIntent().getStringExtra("PK_Dia_Code") == "") {
			diaryCode = "2";
		} else {
			diaryCode = getIntent().getStringExtra("PK_Dia_Code");
		}
		setContentView(R.layout.diarydetail_activity);
		setStartData();
		setImgSetting();
		setStartToolbar();
		startNetwork(FIRST_THREAD);
		registerReceiver(mToastMessageReceiver, new IntentFilter(
				"com.defysoft.waukids.TOAST_MESSAGE"));
	}

	@Override
	public void setStartData() {
		super.setStartData();
		CurYearFormat = new SimpleDateFormat("yyyy"); 
		CurMonthFormat = new SimpleDateFormat("MM");  
		CurDayFormat = new SimpleDateFormat("dd");    
		CurHourFormat = new SimpleDateFormat("HH");   
		CurMinuteFormat = new SimpleDateFormat("mm"); 
		CurSecondFormat = new SimpleDateFormat("ss"); 
		psf = new PublicSharedpreferences(this);
		psf.setMembersp();
		member = psf.getMembersp();		
		psf = new PublicSharedpreferences(this);
		psf.setStudentsp();
		student = psf.getStudentsp();		
		
		ipm =(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
		photoList = new ArrayList<WK_DIARY_PHOTO>();		
		/** 수첩 제목 */
		textView[0] = (TextView) findViewById(R.id.DIARY_DETAIL_NAME);
		/** 수첩 내용 */
		textView[1] = (TextView) findViewById(R.id.DIARY_DETAIL_CONTENTS);
		/** 수첩 지난 시간 */
		textView[2] = (TextView) findViewById(R.id.DIARY_DETAIL_ELAPSED_TIME);
		
		textView[3] = (TextView)findViewById(R.id.DIARY_DETAIL_TITLE);
		/** 수첩 사진 */
		imageView[0] = (ImageView) findViewById(R.id.DIARY_DETAIL_IMG_1);
		imageView[1] = (ImageView) findViewById(R.id.DIARY_DETAIL_IMG_2);
		imageView[2] = (ImageView) findViewById(R.id.DIARY_DETAIL_IMG_3);
		imageView[3] = (ImageView) findViewById(R.id.DIARY_DETAIL_IMG_4);
		imageView[4] = (ImageView) findViewById(R.id.DIARY_DETAIL_IMG_5);
		imageView[5] = (ImageView) findViewById(R.id.DIARY_DETAIL_IMG_6);
		imageView[6] = (ImageView) findViewById(R.id.DIARY_DETAIL_IMG_7);
		imageView[7] = (ImageView) findViewById(R.id.DIARY_DETAIL_IMG_8);
		imageView[8] = (ImageView) findViewById(R.id.DIARY_DETAIL_IMG_9);
		imageView[9] = (ImageView) findViewById(R.id.DIARY_DETAIL_IMG_10);
		// 사진 리니어레이아웃.
		linearLayout[0] = (LinearLayout) findViewById(R.id.DIARY_DETAIL_IMG_1_LL);
		linearLayout[1] = (LinearLayout) findViewById(R.id.DIARY_DETAIL_IMG_2_LL);
		linearLayout[2] = (LinearLayout) findViewById(R.id.DIARY_DETAIL_IMG_3_LL);
		linearLayout[3] = (LinearLayout) findViewById(R.id.DIARY_DETAIL_IMG_4_LL);
		linearLayout[4] = (LinearLayout) findViewById(R.id.DIARY_DETAIL_IMG_5_LL);
		linearLayout[5] = (LinearLayout) findViewById(R.id.DIARY_DETAIL_IMG_6_LL);
		linearLayout[6] = (LinearLayout) findViewById(R.id.DIARY_DETAIL_IMG_7_LL);
		linearLayout[7] = (LinearLayout) findViewById(R.id.DIARY_DETAIL_IMG_8_LL);
		linearLayout[8] = (LinearLayout) findViewById(R.id.DIARY_DETAIL_IMG_9_LL);
		linearLayout[9] = (LinearLayout) findViewById(R.id.DIARY_DETAIL_IMG_10_LL);		
		for (int i = 0; i < 10; i++) {
			final int ii = i;
			imageView[i].setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					intent = new Intent(DiaryDetailActivity.this,
							DiaryDetailPhoto.class).putExtra(
							"photo",
							PublicConstant.HTTP_URL_DIARYPHOTO
									+ photoList.get(ii).Dia_Pho_IMG).putExtra(
							"photoname", photoList.get(ii).Dia_Pho_IMG);
					startActivity(intent);
				}
			});
			sc = new Sqlite_Connect(this);
			sc.delete_New_Diary(Integer.parseInt(diaryCode));
		}
		// 하단 버튼.
		button[0] = (Button) findViewById(R.id.DIARY_DETAIL_BUTTON_REPLY);
		button[1] = (Button) findViewById(R.id.DIARY_DETAIL_BUTTON_STAMP);
		button[0].setOnClickListener(this);
		editText = (EditText)findViewById(R.id.DIARY_DETAIL_EDITTEXT_REPLY);
		// 사진 클릭시
		for (int i = 0; i < 10; i++) {
			final int ii = i;
			imageView[i].setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					intent = new Intent(DiaryDetailActivity.this,
							DiaryDetailPhoto.class).putExtra(
							"photo",
							PublicConstant.HTTP_URL_DIARYPHOTO
									+ photoList.get(ii).Dia_Pho_IMG).putExtra(
							"photoname", photoList.get(ii).Dia_Pho_IMG);
					startActivity(intent);
				}
			});
		}
		
	toolbar = (Toolbar)findViewById(R.id.DIARY_DETAIL_TOOLBAR);
	spinner = (Spinner)findViewById(R.id.DIARY_DETAIL_SPI);
	//댓글용 추가
	replyLinearLayout = (LinearLayout)findViewById(R.id.DIARY_DETAIL_LINEARLAYOUT_REPLY);
	replyListView = (ListView)findViewById(R.id.DIARY_DETAIL_LISTVIEW_REPLY);
	scrollView = (ScrollView)findViewById(R.id.DIARY_DETAIL_SCROLLVIEW);

	
	}
    @Override
    public void startThread(int choice) {
		super.startThread(choice);
		final int cho = choice;
		firstThread = new Thread(new Runnable() {
			@Override
			public void run() {
				switch (cho) {
				case FIRST_THREAD:
					diaryDetailThread();
					break;
				case REPLY_THREAD:
					replyThread();
					break;
				case DELETE_THREAD:
					deleteThread();
					break;


				}
			}
		});
		firstThread.start();
	}

	protected void stampThread() {
		// TODO Auto-generated method stub
		
	}

	protected void diaryDetailThread() {
		String msg = "";
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_DIARY_DETAIL_LOAD + "&PK_Dia_Code="
							+ diaryCode));
			if (jsonObject.getBoolean("success")) {
				diaryDetailLoad = JsonParser.diaryDetailLoad(jsonObject
						.getString("payload"));
				this.photoList = JsonParser.diaryPhotoList(jsonObject
						.getString("photo"));
				this.replyList = JsonParser.diaryReplyList(jsonObject
						.getString("reply"));
			
				this.photo_count = jsonObject.getInt("count");
				this.now_date = jsonObject.getString("nowdate"); // 현재시간
				onNetworkCallback(SUCCESS_FIRST, "");
			} else {
				onNetworkCallback(FAIR_FIRST, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION, e.toString() +" where diaryDetailThread()");
		}
	}

	protected void replyThread() {
		String msg = "";
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_DIARY_DETAIL_REPLY 
					+"&FK_Sch_Dia_Rep_Code=" + diaryDetailLoad.FK_Sch_Dia_Code
					+"&FK_Cla_Dia_Rep_Code=" + diaryDetailLoad.FK_Cla_Dia_Code
					+"&FK_Dia_Dia_Rep_Code="+diaryDetailLoad.PK_Dia_Code 
					+"&FK_Mem_Dia_Rep_Email=" + member.Email
					+"&FK_Stu_Dia_Rep_Code=" + student.Code
					+"&Dia_Rep_Sub="+editText.getText().toString()
					));
			if (jsonObject.getBoolean("success")) {
				onNetworkCallback(SUCCESS_REPLY, "");
			} else {
				onNetworkCallback(FAIR_REPLY, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION, e.toString() +" where replyThread()");
		}
	}

	protected void deleteThread() {
		String msg = "";
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_DIARY_DETAIL_DELETE + "&PK_Dia_Code="
							+ diaryDetailLoad.PK_Dia_Code));
			if (jsonObject.getBoolean("success")) {
				onNetworkCallback(SUCCESS_DELETE,
						jsonObject.getString("payload"));
			} else {
				onNetworkCallback(FAIR_DELETE, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION, e.toString() +" where deleteThread()");
		}
	}

	@Override
    protected void handlingMessage(Message _message) {
    	super.handlingMessage(_message);
    	switch (_message.what) {
		case SUCCESS_FIRST:
			endDIal();
			if (firstThread != null) {firstThread.interrupt();}		
			textView[0].setText(diaryDetailLoad.Dia_Writer);
			textView[1].setText(diaryDetailLoad.Dia_Sub);
			textView[3].setText(diaryDetailLoad.Dia_Name);
			//선생님이 선택한 스탬프 보이게.
			/*if (diaryDetailLoad.Dia_Stamp.equals("0")) {
				button[1].setVisibility(View.INVISIBLE);		
			} else if(diaryDetailLoad.Dia_Stamp.equals("1")){
				button[1].setBackgroundResource(R.drawable.stamp_one);		
			} else if(diaryDetailLoad.Dia_Stamp.equals("2")){
				button[1].setBackgroundResource(R.drawable.stamp_two);		
			} else if(diaryDetailLoad.Dia_Stamp.equals("3")){
				button[1].setBackgroundResource(R.drawable.stamp_three);		
			}*/
			// 작성자만 스피너 보이게.
			if (member.Email.equals(diaryDetailLoad.FK_Mem_Dia_Email)) {
				spinner.setVisibility(View.VISIBLE); // 보이게.
			}
			timeCalculation(); // 경과시간계산기.
			if (photo_count > 10) {
				photo_count = 10;
			}
			if (photo_count > 0) {
				for (int i = 0; i < photo_count; i++) {
					 imageView[i].setVisibility(View.VISIBLE); 
					linearLayout[i].setVisibility(View.VISIBLE);
					imageLoader.displayImage(
							PublicConstant.HTTP_URL_DIARYPHOTO
									+ photoList.get(i).Dia_Pho_IMG,
							imageView[i], options, null);
				}
			}

			//덧글용
			replyAdapter = new DiaryDetailReplyAdapter();
			replyListView.setAdapter(replyAdapter);
			replyListView.setDivider(null);
			//리스트길이보이게
	
			setListViewHeightBasedOnChildren(replyListView);
			
			//강제로 터치스크롤 세팅.1.
			/*replyListView.setOnTouchListener(new OnTouchListener() {
				
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					scrollView.requestDisallowInterceptTouchEvent(true);
					return false;
				}
			});*/
/*			LayoutInflater layoutInflater = getLayoutInflater();
			LinearLayout layout = (LinearLayout)layoutInflater.inflate(R.layout.diarydetail_activity, null);
			replyListView.addFooterView(layout);*/
			//덧글 더보기 함? 안함?
			//스탬프 바꾸기.
				stampChange(diaryDetailLoad.Dia_Stamp);
			break;
		case FAIR_FIRST:
			endDIal();
			if (firstThread != null) {firstThread.interrupt();}	
			Toast.makeText(this, "FAIR_FIRST="+(String) _message.obj, Toast.LENGTH_LONG).show();
			setResult(REFRESH_CODE);
			finish();
			break;
		case SUCCESS_REPLY:
			endDIal();
			if (firstThread != null) {firstThread.interrupt();}	
			startNetwork(FIRST_THREAD);
			setResult(REFRESH_CODE);
			editText.setText("");
			//키보드 내리기.
			ipm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
			
			break;
		case FAIR_REPLY:
			endDIal();
			if (firstThread != null) {firstThread.interrupt();}	
			Toast.makeText(this, "FAIR_REPLY="+(String) _message.obj, Toast.LENGTH_SHORT).show();
			break;
		case SUCCESS_DELETE:
			endDIal();
			if (firstThread != null) {firstThread.interrupt();}	
			Toast.makeText(this, (String) _message.obj, Toast.LENGTH_SHORT).show();
						setResult(REFRESH_CODE);
			finish();
			break;
		case FAIR_DELETE:
			endDIal();
			if (firstThread != null) {firstThread.interrupt();}	
			Toast.makeText(this, "FAIR_DELETE="+(String) _message.obj, Toast.LENGTH_SHORT).show();
			break;
		case CATCH_EXCEPTION:
			endDIal();
			if (firstThread != null) {firstThread.interrupt();}	
			sc.insert_Err_Log((String)_message.obj,android.os.Build.VERSION.SDK_INT,"DiaryDetailActivity", member.Email);
			break;

		}
    }

	private void timeCalculation() {
		// 시간 계산기. split
		
		String album_date = diaryDetailLoad.Dia_Date;
		// 1.앨범사항 시간.
		String alb_date = album_date.split(" ")[0];
		String alb_time = album_date.split(" ")[1];
		// 일월년 분할.
		int alb_year = Integer.parseInt(alb_date.split("-")[0]);
		int alb_month = Integer.parseInt(alb_date.split("-")[1]);
		int alb_day = Integer.parseInt(alb_date.split("-")[2]);
		// 시분초 분할.
		int alb_hour = Integer.parseInt(alb_time.split(":")[0]);
		int alb_minute = Integer.parseInt(alb_time.split(":")[1]);
		int alb_second = Integer.parseInt(alb_time.split(":")[2]);

		// 2. 현재날짜시간
		int now_year = Integer.parseInt(CurYearFormat.format(date));
		int now_month = Integer.parseInt(CurMonthFormat.format(date));
		int now_day = Integer.parseInt(CurDayFormat.format(date));
		int now_hour = Integer.parseInt(CurHourFormat.format(date));
		int now_minute = Integer.parseInt(CurMinuteFormat.format(date));
		int now_second = Integer.parseInt(CurSecondFormat.format(date));

		// 3. 계산기 시작.
		if (alb_year < now_year) {
			textView[2].setText(alb_date);
		} else if (alb_month < now_month) {
			int a = (now_month - alb_month);
			textView[2].setText(a + " 개월 전");
		} else if (alb_day < now_day) {
			int aa = (now_day - alb_day);
			textView[2].setText(aa + " 일 전");
		} else if (alb_hour < now_hour) {
			int aaa = (now_hour - alb_hour);
			textView[2].setText(aaa + " 시간 전");
		} else if (alb_minute < now_minute) {
			int aaaa = (now_minute - alb_minute);
			textView[2].setText(aaaa + " 분 전");
		} else if (alb_minute == now_minute) {
			textView[2].setText("방금 전");

		}
		// 계산기 끝.
		/** 하단 미확인부모, 스탬프3개 */

	}
	private void setListViewHeightBasedOnChildren(ListView replyListView2) {
		ListAdapter listAdapter = replyListView2.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
	
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listitem = listAdapter.getView(i, null, replyListView2);
			listitem.measure(0, 0);
			totalHeight += listitem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = replyListView2.getLayoutParams();
		params.height = totalHeight +(replyListView2.getDividerHeight() * (listAdapter.getCount()-1));
		replyListView2.setLayoutParams(params);
		replyListView2.requestLayout();
	}

	
	private void setImgSetting() {
		// 이미지 세팅.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				this).threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder().cacheInMemory(true)
		/* .imageScaleType(ImageScaleType.EXACTLY) */
		.cacheOnDisc(true).considerExifParams(true).build();
	}

	private void setStartToolbar() {
		toolbar.setNavigationOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		spinner.setAdapter(new ArrayAdapter<String>(this, 0) {
			@Override
			public int getCount() {
				return celebrities.length;
			}

			@Override
			public View getDropDownView(int position, View convertView,
					ViewGroup parent) {
				final int pos = position;
				if (convertView == null) {
					convertView = getLayoutInflater().inflate(
							R.layout.overflowitem_row, null);
				}
				((TextView) convertView.findViewById(R.id.OVERFLOWITEM_TV))
						.setText(celebrities[position]);
				convertView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						switch (pos) {
						case 0: // 수정하기
							Intent i = new Intent(DiaryDetailActivity.this,
									Diary_Detail_Edit.class)
									.putExtra("PK_Dia_Code",
											diaryDetailLoad.PK_Dia_Code);
							startActivityForResult(i, START_DIARY_DETAIL);
							break;
						case 1: // 삭제하기
							alertDialog("삭제 하시겠습니까?", DELETE_THREAD);							
							break;
						}
					}
				});
				return convertView;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				final int pos = position;
				if (convertView == null) {
					convertView = getLayoutInflater().inflate(
							R.layout.overflowitem_row, null);
				}
				((TextView) convertView.findViewById(R.id.OVERFLOWITEM_TV))
						.setBackgroundColor(0x00ffffff);
				return convertView;
			}
		});
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.DIARY_DETAIL_BUTTON_REPLY:		//덧글쓰기.
			if (editText.getText().length() <= 0) {
				Toast.makeText(this, "댓글 내용을 작성해 주세요.",Toast.LENGTH_SHORT).show();
			}else{
				alertDialog(editText.getText().toString(), REPLY_THREAD);
			}
			break;
		}
	}

	// 네.아니오.선택다이얼로그
	void alertDialog(String string_title, int int_code) {
		final String title = string_title;
		final int code = int_code;
		AlertDialog.Builder builder = new AlertDialog.Builder(
				DiaryDetailActivity.this);
		AlertDialog alert;
		builder.setMessage(title)
				.setCancelable(false)
				.setPositiveButton("네", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						startNetwork(code);
					}
				})
				.setNegativeButton("아니오",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
							}
						});
		alert = builder.create();
		alert.show();
	}	

   /** 스탬프에 맞춰 이미지 바꾸기 */ 
	private void stampChange(String dia_Stamp) {
		switch (Integer.parseInt(dia_Stamp)) {
		case 1:
			button[1].setVisibility(View.VISIBLE);
			button[1].setBackgroundResource(R.drawable.stamp_one);
			break;
		case 2:
			button[1].setVisibility(View.VISIBLE);
			button[1].setBackgroundResource(R.drawable.stamp_two);
			
			break;
		case 3:
			button[1].setVisibility(View.VISIBLE);
			button[1].setBackgroundResource(R.drawable.stamp_three);			
			break;

		default:
			button[1].setVisibility(View.INVISIBLE);
			break;
		}
	}
	
//댓글용 어댑터
	class DiaryDetailReplyAdapter extends ArrayAdapter<String> {
		
		public DiaryDetailReplyAdapter(){
			super(DiaryDetailActivity.this, 0);
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return replyList.size();
		}		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			DiaryDetailReplyHolder holder;
			final int pos = position;
			if (convertView == null) {
				holder = new DiaryDetailReplyHolder();
				
					convertView =getLayoutInflater().inflate(R.layout.diary_detail_reply_left_item, null);
					holder.left_ly = (LinearLayout)convertView.findViewById(R.id.DIARY_DETAIL_REPLY_LEFT_CONTAINER);
					holder.left_replySub = (TextView)convertView.findViewById(R.id.DIARY_DETAIL_REPLY_LEFT_SUB);
					holder.left_replyDate = (TextView)convertView.findViewById(R.id.DIARY_DETAIL_REPLY_LEFT_DATE);
					holder.left_replyName = (TextView)convertView.findViewById(R.id.DIARY_DETAIL_REPLY_LEFT_NAME);
					
					holder.right_ly = (LinearLayout)convertView.findViewById(R.id.DIARY_DETAIL_REPLY_RIGHT_CONTAINER);
					holder.right_replySub = (TextView)convertView.findViewById(R.id.DIARY_DETAIL_REPLY_RIGHT_SUB);
					holder.right_replyDate = (TextView)convertView.findViewById(R.id.DIARY_DETAIL_REPLY_RIGHT_DATE);
					holder.right_replyName = (TextView)convertView.findViewById(R.id.DIARY_DETAIL_REPLY_RIGHT_NAME);
				convertView.setTag(holder);
			} else {
				holder = (DiaryDetailReplyHolder) convertView.getTag();
			}
			holder.left_ly.setVisibility(View.GONE);
			holder.right_ly.setVisibility(View.GONE);
			
		
			if(!replyList.get(position).FK_Mem_Dia_Rep_Email.equals(member.Email)){
				holder.right_ly.setVisibility(View.VISIBLE);
				holder.right_replySub.setText(replyList.get(position).Dia_Rep_Sub);
				holder.right_replyDate.setText(replyList.get(position).Dia_Rep_Date);
				holder.right_replyName.setText(replyList.get(position).Dia_Writer);
			
					}else{
			
				holder.left_ly.setVisibility(View.VISIBLE);
				holder.left_replySub.setText(replyList.get(position).Dia_Rep_Sub);
				holder.left_replyDate.setText(replyList.get(position).Dia_Rep_Date);
				holder.left_replyName.setText(replyList.get(position).Dia_Writer);
			}

		return convertView;
		}
		
	}//ADAPTER_END
	class DiaryDetailReplyHolder {
		TextView left_replySub, left_replyDate, left_replyName;
		TextView right_replySub, right_replyDate, right_replyName;
		LinearLayout left_ly , right_ly;
	}	
	
	private final BroadcastReceiver mToastMessageReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			LinearLayout layout = (LinearLayout)getLayoutInflater().inflate(R.layout.toast_layout, null);
			TextView toast_tv = (TextView)layout.findViewById(R.id.TOAST_LAYOUT_TV);
			Toast toast = new Toast(DiaryDetailActivity.this);
			toast.setView(layout);
			toast_tv.setText(intent.getStringExtra("PUSH_NAME")+" : "+intent.getStringExtra("PUSH_SUB"));
			toast.setDuration(Toast.LENGTH_LONG);
			toast.show();
		}
	};
	
}//CLASS_END
